package com.lovepick.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lovepick.R;
import com.lovepick.bean.MsgItem;
import com.lovepick.util.BaseUtils;
import com.lovepick.util.MsgType;

public class MsgItemAdapter extends BaseAdapter
{
	// private final String TAG = "MsgItemAdapter";
	private final int SEND_ITEM_TYPE = 0;
	private final int RECEIVE_ITEM_TYPE = 1;
	private List<MsgItem> mMsgList = null;
	private Context mContext;
	private LayoutInflater mInflater;

	public MsgItemAdapter(Context context, List<MsgItem> list)
	{
		this.mMsgList = list;
		this.mContext = context;
		mInflater = LayoutInflater.from(mContext);
	}

	public int getCount()
	{
		// TODO Auto-generated method stub
		return mMsgList.size();
	}
	
	public void addItem(MsgItem msgItem)
	{
		mMsgList.add(msgItem);		
		notifyDataSetChanged();
	}
	
	public void addAll(List<MsgItem> items)
	{
		mMsgList.addAll(items);
		notifyDataSetChanged();
	}

	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return mMsgList.get(position);
	}

	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	public int getViewTypeCount()
	{
		// TODO Auto-generated method stub
		// 发送消息和接收消息两种视图
		return 2;
	}

	public int getItemViewType(int position)
	{
		// TODO Auto-generated method stub
		MsgItem msgItem = mMsgList.get(position);
		if (MsgType.SEND == msgItem.getMsgType())
		{
			return SEND_ITEM_TYPE;
		}
		else
		{
			return RECEIVE_ITEM_TYPE;
		}

	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		// Log.i(TAG, "-----getView-----=");
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		MsgItem msgItem = mMsgList.get(position);
		MsgType msgType = msgItem.getMsgType();
		// Log.i(TAG, "###sendOrRcv=" + String.valueOf(sendOrRcv) + "###");

		if (convertView == null)
		{
			if (MsgType.SEND == msgType)
			{
				// 消息是发送的
				convertView = mInflater.inflate(R.layout.msg_item_send,
						null);
			}
			else
			{
				convertView = mInflater
						.inflate(R.layout.msg_item_rcv, null);
			}
			viewHolder = new ViewHolder();
			viewHolder.txvDate = (TextView) convertView
					.findViewById(R.id.txv_date);
			viewHolder.txvContent = (TextView) convertView
					.findViewById(R.id.txv_content);

			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.msgType = msgType;
		String date = BaseUtils.getSimpleDate(msgItem.getDate());
		viewHolder.txvDate.setText(date);
		viewHolder.txvContent.setText(msgItem.getContent());

		return convertView;
	}

	static class ViewHolder
	{
		public TextView txvDate;
		public TextView txvContent;
		public MsgType msgType;
	}

}
